using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Framework.Library;
using Framework.Library.Components.Portal;
using Framework.Library.Components.Security;
using Framework.Library.Utilities;
using Framework.Module.CMS.Library.Contents;
using Framework.Module.CMS.Library.Definitions;
using Framework.Module.CMS.Library.Distributions;
using Framework.Library.Components.Commands;
using Framework.Module.CMS.Library.Zones;

namespace Framework.Module.CMS.Admin.WorkFlow
{
	public partial class RejectList : AuthenticatedControl
	{
        private bool isManager, isPublisher;
		protected void Page_Load(object sender, EventArgs e)
		{
            isManager = ZonePermissionController.CheckUserPermision(CurrentUser.UserID, (int)CMSPermision.Manager, ConvertUtility.ToInt32(AppEnv.PortalId()));
            isPublisher = ZonePermissionController.CheckUserPermision(CurrentUser.UserID, (int)CMSPermision.Publisher, ConvertUtility.ToInt32(AppEnv.PortalId()));
            
			if (!Page.IsPostBack) LoadZones();
		}
		private void LoadZones()
		{
            dropZones.Items.Clear();
            ZoneController.LoadZones(dropZones.Items, AppEnv.PortalId());
            dropZones.Items.Insert(0, new ListItem("Root", "0"));
            if (!isManager)
            {
                string permisionZoneCollection = ConvertUtility.ToString(ZonePermissionController.GetPermisionZones(CurrentUser.UserID, ConvertUtility.ToInt32(AppEnv.PortalId())));
                if (permisionZoneCollection != null && permisionZoneCollection.Length > 1)
                {
                    int i = 0;
                    while (i < dropZones.Items.Count)
                    {
                        if (permisionZoneCollection.IndexOf("|" + dropZones.Items[i].Value + "|") < 0)
                            dropZones.Items.RemoveAt(i);
                        else i += 1;
                    }
                }
                else
                {
                    dropZones.Items.Clear();
                }
            }
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			int zoneID = ConvertUtility.ToInt32(dropZones.SelectedValue);            
            if (!isManager && !isPublisher)
            {
                dtgRejectList.DataSource = ContentController.GetContents(zoneID, (int)CMSWorkFlow.Remover, CurrentUser.UserID);
            }
            else
		    {
                dtgRejectList.DataSource = ContentController.GetContents(zoneID, (int)CMSWorkFlow.Remover);
		    }			
            dtgRejectList.DataBind();
		}

        protected void dtgRejectList_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowIndex < 0) return;
			DataRowView curData = (DataRowView)e.Row.DataItem;
			HyperLink lnkHeadline = (HyperLink)e.Row.FindControl("lnkHeadline");
			lnkHeadline.Text = curData["Content_Headline"].ToString();
			lnkHeadline.NavigateUrl = AppEnv.ADMIN_CMD + "cms_viewcontent&contentid=" + curData["Content_ID"].ToString();

			Button cmdDel = (Button)e.Row.FindControl("cmdDel");
			cmdDel.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);

		}

        protected void dtgRejectList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int contentId = ConvertUtility.ToInt32(e.CommandArgument);
            try
            {
                switch (e.CommandName)
                {
                    case "deploy":
                        ContentController.SetContentStatus(contentId, (int)CMSWorkFlow.Deploy);
                        DistributionInfo info = new DistributionInfo();

                        info.Distribution_ContentID = contentId;
                        info.Distribution_ZoneID = ConvertUtility.ToInt32(dropZones.SelectedValue);
                        info.Distribution_Rank = (int)CMSDistributionRank.Default;
                        info.Distribution_ExpireDate = DateTime.Now.AddYears(1);
                        info.Distribution_WaittingDate = DateTime.Now;
                        info.Distribution_WaittingHour = 0;
                        info.Distribution_View = 0;
                        DistributionController.AddDistribution(info);
                        lblUpdateStatus.Text = (Constants.MSG_Deploy + dropZones.SelectedItem.Text);
                        break;
                    case "editcontent":
                        ContentController.LockContent(contentId, CurrentUser.UserID);
                        Response.Redirect(AppEnv.AdminUrlParams(CommandController.GetCommandbyID((int)PortalControls.CMS_EditContent)) + "&contentid=" + contentId + "&inportalid=" + AppEnv.PortalId() + "&inzoneid=" + dropZones.SelectedValue);
                        break;
                    case "deletecontent":
                        ContentController.DeleteContent(contentId);
                        break;
                    default:
                        break;
                }
                lblUpdateStatus.Text = MiscUtility.MSG_UPDATE_SUCCESS;
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        
	}
}